package wfmanage.action;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;


import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.util.DB;
import com.util.MyLog;

import edu.wfmanage.models.Domain;
import edu.wfmanage.models.Lookup;

public class AdminHome extends ActionSupport{

	private String name = "";
	private int id;
	private ArrayList<Domain> domains = new ArrayList<Domain>();
	
	public ArrayList<Domain> getDomains() {
		return domains;
	}
	public void setDomains(ArrayList<Domain> domains) {
		this.domains = domains;
	}
	private ArrayList<Lookup> allValues = new ArrayList<Lookup>();
	private ArrayList<String> domainList = new ArrayList<String>();
	private ArrayList<Lookup> allWFValues = new ArrayList<Lookup>();
	private ArrayList<String> WFList = new ArrayList<String>();
	private ArrayList<Lookup> allRoleValues = new ArrayList<Lookup>();
	private ArrayList<String> roleList = new ArrayList<String>();
	private ArrayList<Lookup> allUserValues = new ArrayList<Lookup>();
	private ArrayList<String> userList = new ArrayList<String>();
	private ArrayList<Lookup> mapValues = new ArrayList<Lookup>();
	Lookup lookup =new Lookup();
	
	
	private Map<String, Object> session;
	private Map<String, Object> domain_wf_mapping;
	private String error="";
	public String getError() {
		return error;
	}
	public void setError(String error) {
		this.error = error;
	}

	public ArrayList<Lookup> getMapValues() {
		return mapValues;
	}
	public void setMapValues(ArrayList<Lookup> mapValues) {
		this.mapValues = mapValues;
	}
	public Map<String, Object> getDomain_wf_mapping() {
		return domain_wf_mapping;
	}
	public void setDomain_wf_mapping(Map<String, Object> domain_wf_mapping) {
		this.domain_wf_mapping = domain_wf_mapping;
	}
	public ArrayList<Lookup> getAllWFValues() {
		return allWFValues;
	}
	public void setAllWFValues(ArrayList<Lookup> allWFValues) {
		this.allWFValues = allWFValues;
	}
	public ArrayList<String> getWFList() {
		return WFList;
	}
	public void setWFList(ArrayList<String> wFList) {
		WFList = wFList;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
    public Lookup getLookup() {
		return lookup;
	}
	public void setLookup(Lookup lookup) {
		this.lookup = lookup;
	}
	public ArrayList<Lookup> getAllValues() {
		return allValues;
	}
	public void setAllValues(ArrayList<Lookup> allValues) {
		this.allValues = allValues;
	}
	public ArrayList<String> getDomainList() {
		return domainList;
	}
	public void setDomainList(ArrayList<String> domainList) {
		this.domainList = domainList;
	}
	public ArrayList<Lookup> getAllRoleValues() {
		return allRoleValues;
	}
	public void setAllRoleValues(ArrayList<Lookup> allRoleValues) {
		this.allRoleValues = allRoleValues;
	}
	public ArrayList<String> getRoleList() {
		return roleList;
	}
	public void setRoleList(ArrayList<String> roleList) {
		this.roleList = roleList;
	}
	public ArrayList<Lookup> getAllUserValues() {
		return allUserValues;
	}
	public void setAllUserValues(ArrayList<Lookup> allUserValues) {
		this.allUserValues = allUserValues;
	}
	public ArrayList<String> getUserList() {
		return userList;
	}
	public void setUserList(ArrayList<String> userList) {
		this.userList = userList;
	}
	public String execute() throws Exception {
		session = ActionContext.getContext().getSession();
		String clicked = ServletActionContext.getRequest().getParameter("link");
		if(clicked.equals("lookups")){
			return "lookups";
		}
		else if(clicked.equals("defineWF")){

			this.allValues=lookup.find("domain"," where is_current=1");
			
			for (int i = 0; i < allValues.size(); i++){
	        	Lookup list = allValues.get(i);
	        	domainList.add(list.getName());
	        	MyLog.log("name:"+list.getName());
	        }
			
			this.allWFValues=lookup.find("WorkflowPoint"," where is_current=1");
			for (int i = 0; i < allWFValues.size(); i++){
	        	Lookup list = allWFValues.get(i);
	        	WFList.add(list.getName());
	        	MyLog.log("name:"+list.getName());
	        }
			return "defineWF";
		}
		else if(clicked.equals("assignRole")){
			
			this.allValues=lookup.find("domain"," where is_current=1");
			
			for (int i = 0; i < allValues.size(); i++){
	        	Lookup list = allValues.get(i);
	        	Domain e=new Domain(list.getId(),list.getName());
	        	domains.add(e);
	        	domainList.add(list.getName());
	        	MyLog.log("name:"+list.getName());
	        	
	        	//this.mapValues=lookup.findWF(" "+list.getId());
	        	//domain_wf_mapping.put(list.getName(), this.mapValues);
	        }
			this.allWFValues=lookup.find("WorkflowPoint"," where is_current=1");
			for (int i = 0; i < allWFValues.size(); i++){
	        	Lookup list = allWFValues.get(i);
	        	WFList.add(list.getName());
	        	MyLog.log("name:"+list.getName());
	        }
			this.allRoleValues=lookup.find("role"," where is_current=1");
			for (int i = 0; i < allRoleValues.size(); i++){
	        	Lookup list = allRoleValues.get(i);
	        	roleList.add(list.getName());
	        	MyLog.log("name:"+list.getName());
	        }
			return "assignRole";
		}
		else if(clicked.equals("tagUser")){
			//domains=new ArrayList();
			this.allValues=lookup.find("domain"," where is_current=1");
			
			for (int i = 0; i < allValues.size(); i++){
	        	Lookup list = allValues.get(i);
	        	Domain e=new Domain(list.getId(),list.getName());
	        	domains.add(e);
	        	domainList.add(list.getName());
	        	MyLog.log("name:"+list.getName());
	        }
			this.allRoleValues=lookup.find("role"," where is_current=1");
			for (int i = 0; i < allRoleValues.size(); i++){
	        	Lookup list = allRoleValues.get(i);
	        	roleList.add(list.getName());
	        	MyLog.log("name:"+list.getName());
	        }
			this.allUserValues=lookup.find("user"," where is_current=1");
			for (int i = 0; i < allUserValues.size(); i++){
	        	Lookup list = allUserValues.get(i);
	        	userList.add(list.getName());
	        	MyLog.log("name:"+list.getName());
	        }
			return "tagUser";
		}
		else if(clicked.equals("assignRules")){
			this.allValues=lookup.find("domain"," where is_current=1");
			
			for (int i = 0; i < allValues.size(); i++){
	        	Lookup list = allValues.get(i);
	        	Domain e=new Domain(list.getId(),list.getName());
	        	domains.add(e);
	        	domainList.add(list.getName());
	        	MyLog.log("name:"+list.getName());
	        }
			MyLog.log("domainlist");
			session.put("domainList", domainList);
			ArrayList<String> test=(ArrayList<String>) session.get("domainList");
			for (int i = 0; i < test.size(); i++){
				MyLog.log("value: "+test.get(i));
			}
			return "assignRules";
		}
		else
			return "error";
	}
	
}
